package com.gwtt.simulator.netconf.subsystem;

import lombok.extern.slf4j.Slf4j;
import org.apache.sshd.common.io.IoInputStream;
import org.apache.sshd.common.io.IoOutputStream;
import org.apache.sshd.server.Environment;
import org.apache.sshd.server.ExitCallback;
import org.apache.sshd.server.channel.ChannelSession;

import java.io.IOException;

@Slf4j
public class NetconfAsyncCommand extends NetconfAsyncCommandAdapter {

    private final String id;
    private final NetconfAsyncClient asyncClient = new NetconfAsyncClient();

    public NetconfAsyncCommand(String id) {
        this.id = id;
        this.asyncClient.setId(id);
    }

    @Override
    public void setIoInputStream(IoInputStream in) {
        log.info("{} start to set io input stream", this.id);
        this.asyncClient.setIn(in);
    }

    @Override
    public void setIoOutputStream(IoOutputStream out) {
        log.info("{} start to set io out stream", this.id);
        this.asyncClient.setOut(out);
    }

    @Override
    public void setIoErrorStream(IoOutputStream err) {

    }

    @Override
    public void setExitCallback(ExitCallback callback) {

    }

    @Override
    public void start(ChannelSession channel, Environment env) throws IOException {
        this.asyncClient.sendHelloMessage();
        this.asyncClient.read();
        log.info("{} ssh client started", this.id);
    }

    @Override
    public void destroy(ChannelSession channel) throws Exception {

    }
}
